package com.example.demo.service;

import com.example.demo.mapper.AccountMapper;
import com.example.demo.pojo.Account;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountService {

    @Autowired
    private AccountMapper accountMapper;

    public List<Account> getAllAccounts(){ return accountMapper.selectAll();}

    public void addNewAccount(Account account){ accountMapper.addAccount(account);}

    public void updateAccount(Account account){ accountMapper.updateAccount(account);}

    public Account getAccountById(Integer id){ return accountMapper.selectAccountById(id);}

    public Account userLogin(String username, String password){
        Account account = accountMapper.checkAccount(username);
        if (account == null|| !password.equals(account.getPwd())){
            return null;
        }else{
            return account;
        }
    }

    public Integer getAuthority(Account account){
        if(account != null){
            return account.getAuthority();
        }
        return  -1;
    }

    public List<Account> getAccountsByConditions(String conditionText, String conditionType) {
        if("".equals(conditionText.trim())){
            conditionText = null;
        }
        switch (conditionType){
            case "权限":
                conditionType = "account_staff.authority";
                break;
            case "用户名":
                conditionType = "account_staff.username";
                break;
            case "员工ID":
                conditionType = "account_staff.s_id";
                break;
            default:
                conditionType = null;
        }
        return accountMapper.getAccountsByConditions(conditionText,conditionType);
    }

//    public String  updatePwd(String pwd_new , String pwd_old, Integer s_id){
//        Account account = accountMapper.selectAccountById(s_id);
//        if(account.getPwd() != pwd_old){
//            return "原密码错误，请重新输入";
//        }else{
//            accountMapper.updatePwd(pwd_new, s_id);
//            return null;
//        }
//
//    }
public void  updatePwd(String pwd_new , String pwd_old, Integer s_id){
    Account account = accountMapper.selectAccountById(s_id);
    accountMapper.updatePwd(pwd_new, s_id);

}
}
